package tech.aistar.service.wxdaoyou.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.aistar.entity.authordis.UserTable;
import tech.aistar.entity.daoyou.GuideApply;
import tech.aistar.entity.daoyou.GuideInfo;
import tech.aistar.entity.person_center.Orders;
import tech.aistar.mapper.userLoginAndRegister.UserLogin;
import tech.aistar.mapper.wxmapper.wxGuide;
import tech.aistar.service.wxdaoyou.Wxservice;
import tech.aistar.util.date_SimpleDateFormat.DateUtileDemo;
import tech.aistar.vo.daoyou.GuideOrdersAll;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class WxServiceImpl implements Wxservice {
    @Autowired
    private UserLogin userLogin;

    @Autowired
    private wxGuide wxGuide;

    //登陆
    @Override
    public UserTable wxgetByPhone(String phone, String password) {
    UserTable table= userLogin.findByPhone(phone,password);
        return table;
    }

//获取账单
    @Override
    public List<GuideOrdersAll> wxgetOrder(String  phone) {
      Integer id=  getUserId(phone);
        List<Orders> ordersList= wxGuide.getBillById(id);
        List<GuideOrdersAll> list=new ArrayList<>();
        Iterator<Orders> ordersIterator=ordersList.iterator();
        while (ordersIterator.hasNext()){
            GuideOrdersAll guideOrdersAll=new GuideOrdersAll ();
            Orders orders=ordersIterator.next();
            String startTime=  DateUtileDemo.format("yyyy-MM-dd",orders.getApplyTime());
            String endTime=   DateUtileDemo.format("yyyy-MM-dd", orders.getPlayDay());
            guideOrdersAll.setStartTime(startTime);
            guideOrdersAll.setEndTime(endTime);
            guideOrdersAll.setOrders(orders);
            list.add(guideOrdersAll);
        }
        return list;
    }

    @Override
    public Integer getGuideId(String token) {
       Integer id= wxGuide.getUserId(token);
      return wxGuide.getGuideId(id);
    }

    @Override
    public GuideApply getGuideById(String token) {
        Integer id= wxGuide.getUserId(token);
        return wxGuide.getGuideById(id);
    }

    @Override
    public Orders getOrders(Integer BillId) {
        Orders orders=  wxGuide.getOrders(BillId);
        return orders;
    }

    @Override
    public String delBill(String orderno, Integer BillId) {
        Orders orders=wxGuide.selectBill(BillId);
        if(orders==null){
            return "error";
        }
        //评论时间
        Date date = new Date();
        String time=DateUtileDemo.format("yyyy-MM-dd HH:mm:ss",date);
        /**
         * 核销时间必须大于等于出行时间
         */
        if(orders.getOrderno().equals(orderno) && (date.getDay()- orders.getApplyTime().getDay() )>=0){
            wxGuide.delBill(BillId,time);
            wxGuide.addGuideScore(orders.getGuideId());//得分


            Integer reputation= wxGuide.getd(orders.getGuideId()).getReputation();
            Integer grade= wxGuide.getd(orders.getGuideId()).getGrade();
            if(reputation/100>grade&&grade<5){
                wxGuide.updaD(orders.getGuideId(),reputation/100);
            }

            return "ok";
        }
        return "error";
    }

    @Override
    public Orders checkBill(Integer billId) {
       Orders orders= wxGuide.getBillStart(billId);
        return orders;
    }

//    评论
    @Override
    public void setOrder(Integer id, String text, Integer contentx_type) {
        Date date = new Date();
        String time=DateUtileDemo.format("yyyy-MM-dd HH:mm:ss",date);
        //获取导游的信息
        Integer guideId=wxGuide.getBillStart(id).getGuideId();
        wxGuide.updateGuideScore(guideId,(contentx_type*2));

         Integer reputation= wxGuide.getd(guideId).getReputation();
         Integer grade= wxGuide.getd(guideId).getGrade();
        if(reputation/100>grade&&grade<5){
            wxGuide.updaD(guideId,reputation/100);
        }
        wxGuide.setOrder(id,time,text,contentx_type);
    }




    //通过token的值 来获取到用户的id
    public Integer getUserId(String token){
        return   wxGuide.getUserId(token);
    }
}
